#!/usr/bin/python
# -*- coding: utf-8 -*-

"""Project Euler Solution 042

Copyright (c) 2011 by Robert Vella - robert.r.h.vella@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import cProfile
from euler.string_functions import alphabetical_value
from euler.numbers.number_theory import istriangle_number
from euler.file_functions import split_file


def get_answer():    
    """Question:
    
    The nth term of the sequence of triangle numbers is given by, 
    t(n) = ½n(n+1); so the first ten triangle numbers are:
    
    1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
    
    By converting each letter in a word to a number corresponding to its 
    alphabetical position and adding these values we form a word value. For 
    example, the word value for SKY is 19 + 11 + 25 = 55 = t(10). If the word 
    value is a triangle number then we shall call the word a triangle word.
    
    Using words.txt, a 16K text file containing nearly two-thousand common 
    English words, how many are triangle words?
    """
    
    def istriangle_word(word):
        """Returns true if the alphabetic value of [word] is a triangle 
        number.
        """
        
        return istriangle_number(alphabetical_value(word))
    
    #Return the result.
    return sum(
              1 for word in split_file("../data/042.txt") 
                if istriangle_word(word.replace("\"", ""))
            )

   
if __name__ == "__main__":
    cProfile.run("print(get_answer())")
